package com.citcc.business.service;

import com.citcc.business.bean.CdzGun;
import com.citcc.business.bean.CdzStake;
import com.citcc.business.mapper.CdzGunMapper;
import com.citcc.business.mapper.CdzStakeMapper;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CdzGunService {

    private final CdzGunMapper cdzGunMapper;
    private final CdzStakeMapper cdzStakeMapper;

    @Autowired
    public CdzGunService(CdzGunMapper cdzGunMapper,CdzStakeMapper cdzStakeMapper){

        this.cdzGunMapper=cdzGunMapper;
        this.cdzStakeMapper=cdzStakeMapper;
    }

    public List<CdzGun> selectAllList(CdzGun gun){
        return cdzGunMapper.selectAllList(gun);
    }

    @Transactional(rollbackFor = Exception.class)
    public int add(CdzGun gun){
        int result=cdzGunMapper.insertSelective(gun);
        if(result>0){
            if(gun.getStakeCode()!=null && !"".equals(gun.getStakeCode())){
                int count=cdzGunMapper.countGun(gun.getStakeCode());
               return cdzStakeMapper.updateGunNum(gun.getStakeCode(),String.valueOf(count));
            }else{
                return result;
            }
        }else{
            return result;
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public int update(CdzGun gun){
        CdzGun beforeGun=this.getCdzGun(gun.getId());
        int flag=cdzGunMapper.updateByPrimaryKeySelective(gun);
        if(flag>0){
            if(gun.getStakeCode()!=null && !"".equals(gun.getStakeCode())){
                //先修改之前的枪数量
                int beforecount=cdzGunMapper.countGun(beforeGun.getStakeCode());
                cdzStakeMapper.updateGunNum(beforeGun.getStakeCode(),String.valueOf(beforecount));
                //更新完成后修改后来的枪数量
                int count=cdzGunMapper.countGun(gun.getStakeCode());
                return cdzStakeMapper.updateGunNum(gun.getStakeCode(),String.valueOf(count));
            }else{
                return flag;
            }
        }else{
            return flag;
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public int delete(String id){
        int result= cdzGunMapper.deleteByPrimaryKey(id);
        if(result>0){
            CdzGun gun=this.getCdzGun(id);
            if(gun.getStakeCode()!=null && !"".equals(gun.getStakeCode())){
                int count=cdzGunMapper.countGun(gun.getStakeCode());
                return cdzStakeMapper.updateGunNum(gun.getStakeCode(),String.valueOf(count));
            }else{
                return result;
            }
        }else{
            return result;
        }
    }

    public CdzGun getCdzGun(String id){
        return cdzGunMapper.selectByPrimaryKey(id);
    }

    public int existStake(String stakeCode){
        return cdzGunMapper.existStake(stakeCode);
    }
    
    public boolean updateStatus(CdzGun record){
    	return cdzGunMapper.updateStatus(record)>0;
    }

    public List<CdzGun> queryByStakeCode(String stakeCode){
        return cdzGunMapper.queryByStakeCode(stakeCode);
    }
    
    public int updateOnLine(CdzStake stake){
        return cdzStakeMapper.updateByPrimaryKeySelective(stake);
    }
    
    public int updateByIsonline(String code, String isOnline){
    	return cdzStakeMapper.updateByIsonline(code, isOnline);
    }

}
